package com.zdxlz.desensitize.service;

import com.zdxlz.desensitize.constant.ColumnType;
import com.zdxlz.desensitize.constant.DesensitizeType;
import com.zdxlz.desensitize.core.IDesensitizeService;
import com.zdxlz.desensitize.domain.DesensitizeRule;
import com.zdxlz.desensitize.domain.DesensitizeRuleDto;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 默认脱敏规则
 */
public class DefaultDesensitizeService implements IDesensitizeService {
    /**
     * KEY的前缀
     */
    private final String keyPrefix;

    public DefaultDesensitizeService(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }

    /**
     * 默认规则
     * @param roles
     */
    @Override
    public List<DesensitizeRule> getRules(List<String> roles) {
        List<DesensitizeRule> rules = new ArrayList<>();
        rules.add(new DesensitizeRule(ColumnType.PHONE, DesensitizeType.PHONE));
        rules.add(new DesensitizeRule(ColumnType.IDCARD, DesensitizeType.IDCARD));
        rules.add(new DesensitizeRule(ColumnType.EMAIL, DesensitizeType.EMAIL));
        rules.add(new DesensitizeRule(ColumnType.ADDRESS, DesensitizeType.ADDRESS));
        rules.add(new DesensitizeRule(ColumnType.OFFICEPASS, DesensitizeType.OFFICEPASS));
        rules.add(new DesensitizeRule(ColumnType.NAME, DesensitizeType.NAME));
        rules.add(new DesensitizeRule(ColumnType.OTHER, DesensitizeType.EXTEND));
        return rules;
    }

    @Override
    public List<DesensitizeRuleDto> getRuleDtos(List<String> roles,String requestUri) {
        List<DesensitizeRuleDto> dtos = new ArrayList<>();
        dtos.add(new DesensitizeRuleDto(keyPrefix+"phone", DesensitizeType.PHONE));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"idcard", DesensitizeType.IDCARD));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"email", DesensitizeType.EMAIL));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"address", DesensitizeType.ADDRESS));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"officepass", DesensitizeType.OFFICEPASS));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"name", DesensitizeType.NAME));
        dtos.add(new DesensitizeRuleDto(keyPrefix+"other", DesensitizeType.EXTEND));
        return dtos;
    }

    @Override
    public List<String> getRoles(HttpServletRequest request) {
        return null;
    }
}
